home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / tos / gdb / gdb_18s.zoo / command.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-25  |  14.5 KB  |  478 lines

  1. /* Library for reading command lines and decoding commands.
  2.    Copyright (C) 1986 Free Software Foundation, Inc.
  3.  
  4.                NO WARRANTY
  5.  
  6.   BECAUSE THIS PROGRAM IS LICENSED FREE OF CHARGE, WE PROVIDE ABSOLUTELY
  7. NO WARRANTY, TO THE EXTENT PERMITTED BY APPLICABLE STATE LAW.  EXCEPT
  8. WHEN OTHERWISE STATED IN WRITING, FREE SOFTWARE FOUNDATION, INC,
  9. RICHARD M. STALLMAN AND/OR OTHER PARTIES PROVIDE THIS PROGRAM "AS IS"
  10. WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
  11. BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS TO THE QUALITY
  13. AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE PROGRAM PROVE
  14. DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR
  15. CORRECTION.
  16.  
  17.  IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW WILL RICHARD M.
  18. STALLMAN, THE FREE SOFTWARE FOUNDATION, INC., AND/OR ANY OTHER PARTY
  19. WHO MAY MODIFY AND REDISTRIBUTE THIS PROGRAM AS PERMITTED BELOW, BE
  20. LIABLE TO YOU FOR DAMAGES, INCLUDING ANY LOST PROFITS, LOST MONIES, OR
  21. OTHER SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
  22. USE OR INABILITY TO USE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR
  23. DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY THIRD PARTIES OR
  24. A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS) THIS
  25. PROGRAM, EVEN IF YOU HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH
  26. DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY.
  27.  
  28.         GENERAL PUBLIC LICENSE TO COPY
  29.  
  30.   1. You may copy and distribute verbatim copies of this source file
  31. as you receive it, in any medium, provided that you conspicuously and
  32. appropriately publish on each copy a valid copyright notice "Copyright
  33. (C) 1986 Free Software Foundation, Inc."; and include following the
  34. copyright notice a verbatim copy of the above disclaimer of warranty
  35. and of this License.  You may charge a distribution fee for the
  36. physical act of transferring a copy.
  37.  
  38.   2. You may modify your copy or copies of this source file or
  39. any portion of it, and copy and distribute such modifications under
  40. the terms of Paragraph 1 above, provided that you also do the following:
  41.  
  42.     a) cause the modified files to carry prominent notices stating
  43.     that you changed the files and the date of any change; and
  44.  
  45.     b) cause the whole of any work that you distribute or publish,
  46.     that in whole or in part contains or is a derivative of this
  47.     program or any part thereof, to be licensed at no charge to all
  48.     third parties on terms identical to those contained in this
  49.     License Agreement (except that you may choose to grant more extensive
  50.     warranty protection to some or all third parties, at your option).
  51.  
  52.     c) You may charge a distribution fee for the physical act of
  53.     transferring a copy, and you may at your option offer warranty
  54.     protection in exchange for a fee.
  55.  
  56. Mere aggregation of another unrelated program with this program (or its
  57. derivative) on a volume of a storage or distribution medium does not bring
  58. the other program under the scope of these terms.
  59.  
  60.   3. You may copy and distribute this program (or a portion or derivative
  61. of it, under Paragraph 2) in object code or executable form under the terms
  62. of Paragraphs 1 and 2 above provided that you also do one of the following:
  63.  
  64.     a) accompany it with the complete corresponding machine-readable
  65.     source code, which must be distributed under the terms of
  66.     Paragraphs 1 and 2 above; or,
  67.  
  68.     b) accompany it with a written offer, valid for at least three
  69.     years, to give any third party free (except for a nominal
  70.     shipping charge) a complete machine-readable copy of the
  71.     corresponding source code, to be distributed under the terms of
  72.     Paragraphs 1 and 2 above; or,
  73.  
  74.     c) accompany it with the information you received as to where the
  75.     corresponding source code may be obtained.  (This alternative is
  76.     allowed only for noncommercial distribution and only if you
  77.     received the program in object code or executable form alone.)
  78.  
  79. For an executable file, complete source code means all the source code for
  80. all modules it contains; but, as a special exception, it need not include
  81. source code for modules which are standard libraries that accompany the
  82. operating system on which the executable file runs.
  83.  
  84.   4. You may not copy, sublicense, distribute or transfer this program
  85. except as expressly provided under this License Agreement.  Any attempt
  86. otherwise to copy, sublicense, distribute or transfer this program is void and
  87. your rights to use the program under this License agreement shall be
  88. automatically terminated.  However, parties who have received computer
  89. software programs from you with this License Agreement will not have
  90. their licenses terminated so long as such parties remain in full compliance.
  91.  
  92.   5. If you wish to incorporate parts of this program into other free
  93. programs whose distribution conditions are different, write to the Free
  94. Software Foundation at 675 Mass Ave, Cambridge, MA 02139.  We have not yet
  95. worked out a simple rule that can be stated here, but we will often permit
  96. this.  We will be guided by the two goals of preserving the free status of
  97. all derivatives of our free software and of promoting the sharing and reuse of
  98. software.
  99.  
  100.  
  101. In other words, you are welcome to use, share and improve this program.
  102. You are forbidden to forbid anyone else to use, share and improve
  103. what you give them.   Help stamp out software-hoarding!  */
  104.  
  105.  
  106. /* Various possibilities for alloca.  */
  107. #ifdef __GNUC__
  108. #define alloca __builtin_alloca
  109. #else
  110. #ifdef sparc
  111. #include <alloca.h>
  112. #else
  113. extern char *alloca ();
  114. #endif
  115. #endif
  116.  
  117. #include "command.h"
  118. #include <stdio.h>
  119.  
  120. extern char *xmalloc ();
  121.  
  122. static char *savestring ();
  123.  
  124. /* Add element named NAME to command list *LIST.
  125.    FUN should be the function to execute the command;
  126.    it will get a character string as argument, with leading
  127.    and trailing blanks already eliminated.
  128.  
  129.    DOC is a documentation string for the command.
  130.    Its first line should be a complete sentence.
  131.    It should start with ? for a command that is an abbreviation
  132.    or with * for a command that most users don't need to know about.  */
  133.  
  134. struct cmd_list_element *
  135. add_cmd (name, class, fun, doc, list)
  136.      char *name;
  137.      int class;
  138.      void (*fun) ();
  139.      char *doc;
  140.      struct cmd_list_element **list;
  141. {
  142.   register struct cmd_list_element *c
  143.     = (struct cmd_list_element *) xmalloc (sizeof (struct cmd_list_element));
  144.  
  145.   delete_cmd (name, list);
  146.   c->next = *list;
  147.   c->name = savestring (name, strlen (name));
  148.   c->class = class;
  149.   c->function = fun;
  150.   c->doc = doc;
  151.   c->prefixlist = 0;
  152.   c->allow_unknown = 0;
  153.   c->abbrev_flag = 0;
  154.   c->aux = 0;
  155.   *list = c;
  156.   return c;
  157. }
  158.  
  159. struct cmd_list_element *
  160. add_alias_cmd (name, oldname, class, abbrev_flag, list)
  161.      char *name;
  162.      char *oldname;
  163.      int class;
  164.      int abbrev_flag;
  165.      struct cmd_list_element **list;
  166. {
  167.   /* Must do this since lookup_cmd tries to side-effect its first arg */
  168.   char *copied_name;
  169.   register struct cmd_list_element *old;
  170.   register struct cmd_list_element *c;
  171.   copied_name = (char *) alloca (strlen (oldname) + 1);
  172.   strcpy (copied_name, oldname);
  173.   old  = lookup_cmd (&copied_name, *list, 0, 1);
  174.  
  175.   if (old == 0)
  176.     {
  177.       delete_cmd (name, list);
  178.       return 0;
  179.     }
  180.  
  181.   c = add_cmd (name, class, old->function, old->doc, list);
  182.   c->prefixlist = old->prefixlist;
  183.   c->prefixname = old->prefixname;
  184.   c->allow_unknown = old->allow_unknown;
  185.   c->abbrev_flag = abbrev_flag;
  186.   c->aux = old->aux;
  187.   return c;
  188. }
  189.  
  190. /* Like add_prefix_cmd but adds an element for a command prefix:
  191.    a name that should be followed by a subcommand to be looked up
  192.    in another command list.  PREFIXLIST should be the address
  193.    of the variable containing that list.  */
  194.  
  195. struct cmd_list_element *
  196. add_prefix_cmd (name, class, fun, doc, prefixlist, prefixname,
  197.         allow_unknown, list)
  198.      char *name;
  199.      int class;
  200.      void (*fun) ();
  201.      char *doc;
  202.      struct cmd_list_element **prefixlist;
  203.      char *prefixname;
  204.      int allow_unknown;
  205.      struct cmd_list_element **list;
  206. {
  207.   register struct cmd_list_element *c = add_cmd (name, class, fun, doc, list);
  208.   c->prefixlist = prefixlist;
  209.   c->prefixname = prefixname;
  210.   c->allow_unknown = allow_unknown;
  211.   return c;
  212. }
  213.  
  214. /* Remove the command named NAME from the command list.  */
  215.  
  216. void
  217. delete_cmd (name, list)
  218.      char *name;
  219.      struct cmd_list_element **list;
  220. {
  221.   register struct cmd_list_element *c;
  222.  
  223.   while (*list && !strcmp ((*list)->name, name))
  224.     {
  225.       *list = (*list)->next;
  226.     }
  227.  
  228.   if (*list)
  229.     for (c = *list; c->next;)
  230.       {
  231.     if (!strcmp (c->next->name, name))
  232.       c->next = c->next->next;
  233.     else
  234.       c = c->next;
  235.       }
  236. }
  237.  
  238. /* Implement a help command on command list LIST.
  239.    COMMAND is the argument given (a command from the list to document)
  240.    or zero for no arg (describe briefly all the commands in the list).
  241.    CMDTYPE is a string to use in the error message if command COMMAND
  242.    is not found in the list.  */
  243.  
  244. /* CLASS should be -1 to list all commands in LIST,
  245.    or a nonnegative class number value to list just commands in that class,
  246.    or -2 to list the classes themselves.  */
  247.  
  248. void
  249. help_cmd (command, list, cmdtype, class, stream)
  250.      char *command;
  251.      struct cmd_list_element *list;
  252.      char *cmdtype;
  253.      int class;
  254.      FILE *stream;
  255. {
  256.   register struct cmd_list_element *c;
  257.   register char *p;
  258.   register int ncmds;
  259.   struct cmdvec { struct cmd_list_element *cmd; int class; };
  260.   register struct cmdvec *cmdvec;
  261.   char *cmdtype1, *cmdtype2;
  262.   int len;
  263.  
  264.   if (command)
  265.     {
  266.       c = lookup_cmd (&command, list, cmdtype, 0);
  267.       if (c == 0)
  268.     return;
  269.  
  270.       /* There are three cases here.
  271.      If c->prefixlist is nonzer, we have a prefix command.
  272.      Print its documentation, then list its subcommands.
  273.  
  274.      If c->function is nonzero, we really have a command.
  275.      Print its documentation and return.
  276.  
  277.      If c->function is zero, we have a class name.
  278.      Print its documentation (as if it were a command)
  279.      and then set class to he number of this class
  280.      so that the commands in the class will be listed.  */
  281.  
  282.       p = c->doc;
  283.       fprintf_filtered (stream, "%s\n", p);
  284.       if (c->function != 0 && c->prefixlist == 0)
  285.     return;
  286.       fputc_filtered ('\n', stream);
  287.       if (c->prefixlist)
  288.     {
  289.       list = *c->prefixlist;
  290.       class = 0;
  291.       cmdtype = c->prefixname;
  292.     }
  293.       else
  294.     class = c->class;
  295.     }
  296.  
  297.   /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub"  */
  298.   len = strlen (cmdtype);
  299.   cmdtype1 = (char *) alloca (len + 1);
  300.   cmdtype1[0] = 0;
  301.   cmdtype2 = (char *) alloca (len + 4);
  302.   cmdtype2[0] = 0;
  303.   if (len)
  304.     {
  305.       cmdtype1[0] = ' ';
  306.       strncpy (cmdtype1 + 1, cmdtype, len - 1);
  307.       cmdtype1[len] = 0;
  308.       strncpy (cmdtype2, cmdtype, len - 1);
  309.       strcpy (cmdtype2 + len - 1, " sub");
  310.     }
  311.  
  312.   if (class == -2)
  313.     fprintf_filtered (stream, "List of classes of %scommands:\n\n", cmdtype2);
  314.   else
  315.     fprintf_filtered (stream, "List of %scommands:\n\n", cmdtype2);
  316.  
  317.   for (c = list; c; c = c->next)
  318.     {
  319.       if (c->abbrev_flag == 0
  320.       && (class == -1    /* Listing all */
  321.           || (c->class == class && c->function != 0) /* Listing one class */
  322.           || (class == -2 && c->function == 0)))    /* Listing the classes */
  323.     {
  324.       fprintf_filtered (stream, "%s -- ", c->name);
  325.       /* Print just first line of documentation.  */
  326.       p = c->doc;
  327.       while (*p && *p != '\n') fputc_filtered(*p++, stream);
  328. /*      fwrite (c->doc, 1, p - c->doc, stream); */
  329.       fputc_filtered ('\n', stream);
  330.     }
  331.     }
  332.  
  333.   if (class == -2)
  334.     fprintf_filtered (stream, "\n\
  335. Type \"help%s\" followed by a class name for a list of commands in that class.",
  336.          cmdtype1);
  337.  
  338.   fprintf_filtered (stream, "\n\
  339. Type \"help%s\" followed by %scommand name for full documentation.\n\
  340. Command name abbreviations are allowed if unambiguous.\n",
  341.        cmdtype1, cmdtype2);
  342. }
  343.  
  344. /* Look up the contents of *LINE as a command in the command list LIST.
  345.    LIST is a chain of struct cmd_list_element's.
  346.    If it is found, return the struct cmd_list_element for that command
  347.    and update *LINE to point after the command name, at the first argument.
  348.    If not found, call error if ALLOW_UNKNOWN is zero
  349.    otherwise (or if error returns) return zero.
  350.    Call error if specified command is ambiguous,
  351.    unless ALLOW_UNKNOWN is negative.
  352.    CMDTYPE precedes the word "command" in the error message.  */
  353.  
  354. struct cmd_list_element *
  355. lookup_cmd (line, list, cmdtype, allow_unknown)
  356.      char **line;
  357.      struct cmd_list_element *list;
  358.      char *cmdtype;
  359.      int allow_unknown;
  360. {
  361.   register char *p;
  362.   register struct cmd_list_element *c, *found;
  363.   int nfound;
  364.   char ambbuf[100];
  365.  
  366.   /* Skip leading whitespace.  */
  367.  
  368.   while (**line == ' ' || **line == '\t')
  369.     (*line)++;
  370.  
  371.   /* Clear out trailing whitespace.  */
  372.  
  373.   p = *line + strlen (*line);
  374. #ifdef atarist
  375.   while (p != *line && (p[-1] == ' ' || p[-1] == '\t' || p[-1] == '\r'))
  376. #else
  377.   while (p != *line && (p[-1] == ' ' || p[-1] == '\t'))
  378. #endif
  379.     p--;
  380.   *p = 0;
  381.  
  382.   /* Find end of command name.  */
  383.  
  384.   p = *line;
  385.   while (*p == '-'
  386.      || (*p >= 'a' && *p <= 'z')
  387.      || (*p >= 'A' && *p <= 'Z')
  388.      || (*p >= '0' && *p <= '9'))
  389.     {
  390.       if (*p >= 'A' && *p <= 'Z')
  391.     *p += 'a' - 'A';
  392.       p++;
  393.     }
  394.  
  395.   /* Look up the command name.
  396.      If exact match, keep that.
  397.      Otherwise, take command abbreviated, if unique.  */
  398.  
  399.   found = 0;
  400.   nfound = 0;
  401.   for (c = list; c; c = c->next)
  402.     {
  403.       if (!strncmp (*line, c->name, p - *line))
  404.     {
  405.       found = c;
  406.       nfound++;
  407.       if (c->name[p - *line] == 0)
  408.         {
  409.           nfound = 1;
  410.           break;
  411.         }
  412.     }
  413.     }
  414.  
  415.   /* Report error for undefined command name.  */
  416.  
  417.   if (nfound != 1)
  418.     {
  419.       if (nfound > 1 && allow_unknown >= 0)
  420.     {
  421.       *p = 0;
  422.       ambbuf[0] = 0;
  423.       for (c = list; c; c = c->next)
  424.         if (!strncmp (*line, c->name, p - *line))
  425.           {
  426.         if (strlen (ambbuf) + strlen (c->name) + 6 < sizeof ambbuf)
  427.           {
  428.             if (strlen (ambbuf))
  429.               strcat (ambbuf, ", ");
  430.             strcat (ambbuf, c->name);
  431.           }
  432.         else
  433.           {
  434.             strcat (ambbuf, "..");
  435.             break;
  436.           }
  437.           }
  438.       error ("Ambiguous %scommand \"%s\": %s.", cmdtype, *line, ambbuf);
  439.     }
  440.       else if (!allow_unknown)
  441.     {
  442.       *p = 0;
  443.       error ("Undefined %scommand: \"%s\".", cmdtype, *line);
  444.     }
  445.       return 0;
  446.     }
  447.  
  448.   /* Skip whitespace before the argument.  */
  449.  
  450.   while (*p == ' ' || *p == '\t') p++;
  451.   *line = p;
  452.  
  453.   if (found->prefixlist && *p)
  454.     {
  455.       c = lookup_cmd (line, *found->prefixlist, found->prefixname,
  456.               found->allow_unknown);
  457.       if (c)
  458.     return c;
  459.     }
  460.  
  461.   return found;
  462. }
  463.  
  464. /* Make a copy of the string at PTR with SIZE characters
  465.    (and add a null character at the end in the copy).
  466.    Uses malloc to get the space.  Returns the address of the copy.  */
  467.  
  468. static char *
  469. savestring (ptr, size)
  470.      char *ptr;
  471.      int size;
  472. {
  473.   register char *p = (char *) xmalloc (size + 1);
  474.   bcopy (ptr, p, size);
  475.   p[size] = 0;
  476.   return p;
  477. }
  478.